package com.aiyige.page.coupon;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.ApiManager;
import com.aiyige.model.Page;
import com.aiyige.page.coupon.adapter.HistoryCouponAdapter;
import com.aiyige.page.coupon.model.UserCoupon;
import com.aiyige.utils.UIHelper;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

@Route(path = ARouterConfig.HistoryCouponPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class HistoryCouponPage extends BaseActivity implements CommonDataView.RetrofitStub {

    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.cdv)
    CommonDataView cdv;
    HistoryCouponAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_history_coupon);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.history_coupon);
        adapter = new HistoryCouponAdapter();
        cdv.getRecyclerView().addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(UIHelper.dip2px(15), UIHelper.dip2px(15), UIHelper.dip2px(15), 0);
            }
        });
        cdv.config(adapter, this);
        cdv.doRefreshRequest();
    }

    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().userHistoryCoupon(currentPage, 20);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            List<UserCoupon> userCouponList = new LinkedList<>();
            if (!TextUtils.isEmpty(page.getContent())) {
                userCouponList.addAll(JSON.parseArray(page.getContent(), UserCoupon.class));
            }
            for (UserCoupon item : userCouponList) {
                if (item == null) continue;
                UserCoupon safeItem = new UserCoupon(item);
                resultList.add(safeItem);
            }
            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
